const cron = require("node-cron");
const NotificationService = require("./notificationService");

class SchedulerService {
  static start() {
    // 每天早上9点检查截止时间提醒
    cron.schedule("0 9 * * *", async () => {
      console.log("开始检查任务截止时间提醒...");
      try {
        await NotificationService.createDueDateReminders();
        console.log("截止时间提醒检查完成");
      } catch (error) {
        console.error("截止时间提醒检查失败:", error);
      }
    });

    // 每小时检查一次即将到期的任务
    cron.schedule("0 * * * *", async () => {
      console.log("检查即将到期的任务...");
      try {
        await NotificationService.createDueDateReminders();
      } catch (error) {
        console.error("检查即将到期任务失败:", error);
      }
    });

    console.log("定时任务调度器已启动");
  }

  static stop() {
    cron.destroy();
    console.log("定时任务调度器已停止");
  }
}

module.exports = SchedulerService;
